﻿function gotoNotePage(id, page) {
    $("#loaderIndicator").show();
    $("#noteListAjax").fadeOut(300);
    $.ajax(
        {
            type: "POST",
            url: "/Account/NotesPartial",
            data: { username: id, currentPage: page },
            success: function (result) {
                $("#noteListAjax").html(result);
                $("#noteListAjax").fadeIn(300);
                $("#loaderIndicator").hide();
            },
            error: function (error) {
                alert(error);
            }
        });
}

function jumpToNotePage(pageElement, user) {
    var pageNumber = $(pageElement).val();
    gotoNotePage(user, pageNumber);
}

function deleteNote(user, page, idItem) {
    $.msgbox("Are you sure that you want to permanently delete the selected element?", {
        type: "info",
        buttons: [
          { type: "submit", value: "Yes" },
          { type: "submit", value: "No" },
        ]
    }, function (result) {
        if (result == "Yes") {
            $("#noteItem-" + idItem).toggleClass("deletingItem");
            $("#noteListAjax").fadeOut(300);
            $.ajax(
                {
                    type: "POST",
                    url: "/Account/DeleteNotes",
                    data: { username: user, currentPage: page, id:idItem },
                    success: function (result) {
                        $("#noteListAjax").html(result);
                        $("#noteListAjax").fadeIn(300);
                        $("#loaderIndicator").hide();
                    }
                });
        }
    });
}

function postNote(id) {
    $("#errorMessage").html("");
    var obj = $("#noteContentId").val();
    if (obj.length < 10) {
        $("#errorMessage").html("Comment can't be less than 10 characters.");
        return;
    }
    $("#loaderIndicator").fadeIn(300);
    $("#postItCommand").fadeOut(300);
    $("#noteListAjax").fadeOut(300);
    $.ajax(
        {
            type: "POST",
            url: "/Account/CreateNotes",
            data: { username: id, noteContent: obj },
            success: function (result) {
                $("#noteListAjax").html(result);
                $("#noteListAjax").fadeIn(300);
                $("#loaderIndicator").hide();
                $("noteContentId").val("");
                $("#postItCommand").fadeIn(100);
            },
            error: function (error) {
                $("#errorMessage").html("Error");
                $("#noteListAjax").fadeIn(300);
                $("#postItCommand").show(100);
            }
        });
}